<?php

/**
 * This is the model class for table "usuario".
 *
 * The followings are the available columns in table 'usuario':
 * @property integer $idUsuario
 * @property string $nombre
 * @property string $apellidoPaterno
 * @property string $apellidoMaterno
 * @property string $username
 * @property string $password
 * @property string $sesion
 * @property string $tipoUsuario
 * @property integer $estado_idEstado
 *
 * The followings are the available model relations:
 * @property Colegios[] $colegioses
 * @property Disponibilidad[] $disponibilidads
 * @property Evaluacion[] $evaluacions
 * @property Estado $estadoIdEstado
 */
class Usuario extends CActiveRecord {

    /**
     * @return string the associated database table name
     */
    public function tableName() {
        return 'usuario';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules() {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('estado_idEstado', 'required'),
            array('nombre', 'required'),
            array('apellidoPaterno', 'required'),
            array('apellidoMaterno', 'required'),
            array('username', 'required'),
            array('password', 'required'),
            array('tipoUsuario', 'required'),
            array('estado_idEstado', 'numerical', 'integerOnly' => true),
            array('nombre, apellidoPaterno, apellidoMaterno', 'length', 'max' => 100),
            array('username, tipoUsuario', 'length', 'max' => 50),
            array('password, sesion', 'length', 'max' => 50),
            // The following rule is used by search().
            // @todo Please remove those attributes that should not be searched.
            array('idUsuario, nombre, apellidoPaterno, apellidoMaterno, username, password, sesion, tipoUsuario, estado_idEstado', 'safe', 'on' => 'search'),
        );
    }

    /**
     * @return array relational rules.
     */
    public function relations() {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
            'colegioses' => array(self::MANY_MANY, 'Colegios', 'colegios_has_usuario(usuario_idUsuario, colegios_idColegios)'),
            'disponibilidads' => array(self::HAS_MANY, 'Disponibilidad', 'persona_idPersona'),
            'evaluacions' => array(self::MANY_MANY, 'Evaluacion', 'evaluacion_has_usuario(usuario_idUsuario, evaluacion_idEvaluacion)'),
            'estadoIdEstado' => array(self::BELONGS_TO, 'Estado', 'estado_idEstado'),
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels() {
        return array(
            'idUsuario' => 'Id Usuario',
            'nombre' => 'Nombre',
            'apellidoPaterno' => 'Apellido Paterno',
            'apellidoMaterno' => 'Apellido Materno',
            'username' => 'Username',
            'password' => 'Password',
            'sesion' => 'Sesion',
            'tipoUsuario' => 'Tipo Usuario',
            'estado_idEstado' => 'Estado',
        );
    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     *
     * Typical usecase:
     * - Initialize the model fields with values from filter form.
     * - Execute this method to get CActiveDataProvider instance which will filter
     * models according to data in model fields.
     * - Pass data provider to CGridView, CListView or any similar widget.
     *
     * @return CActiveDataProvider the data provider that can return the models
     * based on the search/filter conditions.
     */
    public function search() {
        // @todo Please modify the following code to remove attributes that should not be searched.

        $criteria = new CDbCriteria;

        $criteria->compare('idUsuario', $this->idUsuario);
        $criteria->compare('nombre', $this->nombre, true);
        $criteria->compare('apellidoPaterno', $this->apellidoPaterno, true);
        $criteria->compare('apellidoMaterno', $this->apellidoMaterno, true);
        $criteria->compare('username', $this->username, true);
        $criteria->compare('password', $this->password, true);
        $criteria->compare('sesion', $this->sesion, true);
        $criteria->compare('tipoUsuario', $this->tipoUsuario, true);
        $criteria->compare('estado_idEstado', $this->estado_idEstado);

        return new CActiveDataProvider($this, array(
            'criteria' => $criteria,
        ));
    }

    /**
     * Returns the static model of the specified AR class.
     * Please note that you should have this exact method in all your CActiveRecord descendants!
     * @param string $className active record class name.
     * @return Usuario the static model class
     */
    public static function model($className = __CLASS__) {
        return parent::model($className);
    }

    public function validatePassword($password) {
        return $this->hashPassword($password) === $this->password;
    }

    public function hashPassword($password) {
        return md5($password);
    }

}
